# -*- coding: utf-8 -*-

try:
    from spider import Gather, Parser
except ImportError:
    from __init__ import Gather, Parser
import re
import requests
from config import BaseConfig

class BaseSiteIndex(Gather, Parser):
    SEARCH_URL = None

    def __init__(self, domain):
        if domain.startswith("http://"):
            self.domain = domain[7:]
        elif domain.startswith("https://"):
            self.domain = domain[8:]
        else:
            self.domain = domain

        url = self.SEARCH_URL % self.domain
        Gather.__init__(self, url)
        self.set_headers({"accept-language": "zh-CN"})
        Parser.__init__(self, self.gather())

    def get_nums(self):
        nums = self.parser()
        return nums

    def str2int(self, string):
        return int(filter(lambda x: x.isdigit(), string))


class BaiduSiteIndex(BaseSiteIndex):

    SEARCH_URL = "http://www.baidu.com/s?wd=site:%s"

    def parser(self):
        soup = self._soup()
        p = soup.find('p', attrs={"class": "site_tip"})
        if p:
            nums = self.str2int(p.strong.text)
            return nums

        return 0


class GoogleSiteIndex(BaseSiteIndex):

    SEARCH_URL = "http://www.google.com.hk/search?q=site:%s"

    def parser(self):
        soup = self._soup()
        div = soup.find("div", attrs={"id": "resultStats"})
        if div:
            text = re.compile(ur"约 [0-9,]+ 条").findall(div.text)
            try:
                nums = self.str2int(text[0])
            except IndexError:
                url = "http://api.k780.com:88/?app=entry.baidu&domain=%s&appkey=%s&sign=%s&format=json" %(
                        self.domain, BaseConfig.K780KEY, BaseConfig.K780SIGN
                        )
                r = requests.get(url)
                rv = r.json()
                if rv["success"] != "1":
                    return 0
                return rv["result"]["entry"]
            return nums
        return 0


class BingSiteIndex(BaseSiteIndex):

    SEARCH_URL = "http://cn.bing.com/search?q=site:%s"

    def parser(self):
        soup = self._soup()
        span = soup.find("span", attrs={"class": "sb_count"})
        if span:
            nums = self.str2int(span.text)
            return nums
        return 0


class SogouSiteIndex(BaseSiteIndex):

    SEARCH_URL = "http://www.sogou.com/web?query=site:%s"

    def parser(self):
        soup = self._soup()
        h3 =  soup.find("h3", attrs={"class": "pt zz_tit"})
        if h3:
            nums = self.str2int(h3.text)
            return nums
        return 0


class SoSiteIndex(BaseSiteIndex):

    SEARCH_URL = "http://www.so.com/s?q=site:%s"

    def parser(self):
        soup = self._soup()
        p = soup.find('p', attrs={"class": "ws-total"})
        if p:
            text = re.compile(ur"[0-9,]+").findall(p.text)
            nums = self.str2int(text[0])
            return nums
        return 0


if __name__ == '__main__':
    baidu = BaiduSiteIndex("https://www.qq.com")
    google = GoogleSiteIndex("qq.com")
    bing = BingSiteIndex("qq.com")
    sogou = SogouSiteIndex("qq.com")
    so = SoSiteIndex("www.qq.com")
    print so.get_nums()
